package com.huashi.dealer.commonEntity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 用户记录表
 * 对应数据库表：yoshop_user
 * 
 * @author 系统生成
 * @since 2025-01-27
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("yoshop_user")
@Schema(description = "用户记录表")
public class User extends BaseModel implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 用户id
     */
    @TableId(value = "user_id", type = IdType.AUTO)
    @Schema(description = "用户id")
    private Long userId;

    /**
     * 业务员id
     */
    @TableField("store_user_id")
    @Schema(description = "业务员id")
    private Long storeUserId;

    /**
     * 微信openid(唯一标示)
     */
    @TableField("open_id")
    @Schema(description = "微信openid(唯一标示)")
    private String openId;

    /**
     * 微信昵称
     */
    @TableField("nick_name")
    @Schema(description = "微信昵称")
    private String nickName;

    /**
     * 微信头像
     */
    @TableField("avatar_url")
    @Schema(description = "微信头像")
    private String avatarUrl;

    /**
     * 性别
     */
    @Schema(description = "性别：0未知，1男，2女")
    private Integer gender;

    /**
     * 国家
     */
    @Schema(description = "国家")
    private String country;

    /**
     * 省份
     */
    @TableField("province_id")
    @Schema(description = "省份")
    private Integer provinceId;

    /**
     * 城市
     */
    @TableField("city_id")
    @Schema(description = "城市")
    private Integer cityId;

    /**
     * 区域id
     */
    @TableField("region_id")
    @Schema(description = "区域id")
    private Integer regionId;

    /**
     * 默认收货地址
     */
    @TableField("address_id")
    @Schema(description = "默认收货地址")
    private Integer addressId;

    /**
     * 用户可用余额
     */
    @Schema(description = "用户可用余额")
    private BigDecimal balance;

    /**
     * 返利余额
     */
    @Schema(description = "返利余额")
    private BigDecimal rebate;

    /**
     * 核销账户
     */
    @Schema(description = "核销账户")
    private BigDecimal writeoff;

    /**
     * 用户可用积分
     */
    @Schema(description = "用户可用积分")
    private Integer points;

    /**
     * 用户总支付的金额
     */
    @TableField("pay_money")
    @Schema(description = "用户总支付的金额")
    private BigDecimal payMoney;

    /**
     * 实际消费的金额(不含退款)
     */
    @TableField("expend_money")
    @Schema(description = "实际消费的金额(不含退款)")
    private BigDecimal expendMoney;

    /**
     * 销售额（本年目标）
     */
    @TableField("sales_money")
    @Schema(description = "销售额（本年目标）")
    private BigDecimal salesMoney;
    /**
     * 软删除
     */
    @TableField("is_delete")
    @Schema(description = "软删除")
    @TableLogic
    private Integer isDelete;
    /**
     * 会员等级id
     */
    @TableField("grade_id")
    @Schema(description = "会员等级id")
    private Integer gradeId;

    /**
     * 不可查看商品id
     */
    @TableField("goods_ids")
    @Schema(description = "不可查看商品id")
    private String goodsIds;
    
    /**
     * token
     */
    @Schema(description = "token")
    private String token;

    /**
     * 手机号
     */
    @Schema(description = "手机号")
    private String mobile;

    /**
     * 联系人
     */
    @TableField("real_name")
    @Schema(description = "联系人")
    private String realName;

    /**
     * 账号
     */
    @Schema(description = "账号")
    private String account;

    /**
     * 密码
     */
    @Schema(description = "密码")
    private String password;

    /**
     * 性别文本
     */
    @TableField(exist = false)
    @Schema(description = "性别文本")
    private String genderText;

    /**
     * 百分比
     */
    @TableField(exist = false)
    @Schema(description = "百分比")
    private String percentage;

    /**
     * 创建时间
     */
    @TableField("create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "创建时间")
    private Long createTime;

    public void setUpdateTime(Long updateTime) {
        this.updateTime = updateTime;
        this.setUpdateTimeFormat(LocalDateTime.ofEpochSecond(updateTime, 0, java.time.ZoneOffset.ofHours(8)));
    }

    public void setCreateTime(Long createTime) {
        this.createTime = createTime;
        this.setCreateTimeFormat(LocalDateTime.ofEpochSecond(createTime, 0, java.time.ZoneOffset.ofHours(8)));
    }

    /**
     * 更新时间
     */
    @TableField("update_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "更新时间")
    private Long updateTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(exist = false)
    @Schema(description = "创建时间")
    private LocalDateTime createTimeFormat;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "更新时间")
    @TableField(exist = false)
    private LocalDateTime updateTimeFormat;



    /**
     * 获取性别文本
     */
    public String getGenderText() {
        if (gender == null) {
            return "未知";
        }
        switch (gender) {
            case 1:
                return "男";
            case 2:
                return "女";
            default:
                return "未知";
        }
    }

    /**
     * 获取百分比
     */
    public String getPercentage() {
        if (salesMoney == null || salesMoney.compareTo(BigDecimal.ZERO) == 0) {
            return "0%";
        }
        if (payMoney == null || payMoney.compareTo(BigDecimal.ZERO) == 0) {
            return "0%";
        }
        
        double percentage = (payMoney.doubleValue() / salesMoney.doubleValue()) * 100;
        if (percentage >= 100) {
            return "100%";
        } else {
            return String.format("%.1f%%", percentage);
        }
    }
} 